<?php defined('RUN_KATANA') or die('Restricted access'); ?>
<?php echo $commander->getListCommandsPanel('n-sa-a-d-ad_types-ad_cats-sqlpager-sqlfilter-reload', ''); ?>
<div class="menu_list">
	<table style="clear:both;" id="menulist" class="item_list" cellspacing="0" cellpadding="0" border="0">
		<thead class="fields">
			<tr>
				<th class="first center">&nbsp;&nbsp;Id&nbsp;&nbsp;</th>
				<th class="nosort"><input title="<?php echo ucfirst($txt->getWord('all')); ?>" class="select_all" type="checkbox" name="select_all" value="" /></th>
				<th><?php echo ucfirst($txt->getWord('title')); ?></th>
				<th class="nosort"><?php echo ucfirst($txt->getWord('ad_type')); ?></th>
				<th class="nosort"><?php echo ucfirst($txt->getWord('ad_category')); ?></th>
				<th><?php echo ucfirst($txt->getWord('order')); ?></th>
				<th><?php echo ucfirst($txt->getWord('active')); ?></th>
				<th><?php echo ucfirst($txt->getWord('edit')); ?></th>
				<?php if($adc->getSetting('BrandImages')): ?>
                <th><?php echo ucfirst($txt->getWord('image')); ?></th>
				<?php endif; ?>
				<th class="last nosort"><?php echo ucfirst($txt->getWord('delete')); ?></th>
			</tr>
		</thead>
		<tbody>
		</tbody>
	</table>
</div>
<table id="object-list">
</table>
<div id="object-list-pager"></div>
<div id="addDialog" title="<?php echo ucfirst($txt->getWord('new').' '.$txt->getWord('brand')); ?>?" style="display:none; text-align:left;">
	<p> <?php echo ucfirst($txt->getWord('title')); ?>
		<input class="small" name="newTitle" type="text" value="" maxlength="80" id="newTitle" />
		<br />
	</p>
</div>
<div id="editDialog" title="<?php echo ucfirst($txt->getWord('edit').' '.$txt->getWord('brand')); ?>?" style="display:none; text-align:left;">
	<div class="edit-form"></div>
</div>
<div id="delete_dialog" title="<?php echo ucfirst($txt->getWord('delete').' '.$txt->getWord('brand')); ?>?" style="display:none; text-align:left;">
	<p><span class="ui-icon ui-icon-alert" style="float:left; margin:0 7px 20px 0;"></span><?php echo ucfirst($txt->getWord('sure_you_want_to_delete')); ?></p>
</div>
<script type="text/javascript">
function addNewDialog(itemId)
{
	jQuery('#addDialog').dialog('open');
	jQuery('#addDialog').dialog({
		bgiframe: true,
		resizable: false,
		height:140,
		modal: true,
		overlay: 
		{
			backgroundColor: '#000',
			opacity: 0.5
		},
		buttons: 
		{
			'<?php echo ucfirst($txt->getWord('cancel')); ?>': function() 
			{
				jQuery(this).dialog('close');
			},
			'<?php echo ucfirst($txt->getWord('add')).' '.$txt->getWord('element'); ?>': function() 
			{
				jQuery.post("ajax.php?con=<?php echo $_GET['con']; ?>&list=<?php echo $_GET['list']; ?>", 
				{ 
					action:'add', 
					Title: jQuery('#newTitle').val()} 
				).success(function() { loadItems(<?php echo $adc->Pager->getPageCurrent(); ?>); });
				jQuery(this).dialog('close');
			}
		}
	});
}

function editDialog(itemId)
{
	jQuery('#editDialog').dialog('open');
	
	jQuery.ajax(
	{
		url: "ajax.php?con=<?php echo $_GET['con']; ?>&editor=adbrand&adbrand="+itemId,
		cache: false,
	  	success: function(html)
	  	{ 
			jQuery('#editDialog .edit-form').empty();
			jQuery("#editDialog .edit-form").append(html);
		}
	});
	
	jQuery('#editDialog').dialog({
		bgiframe: true,
		resizable: true,
		draggable: true,
		modal: true,
		closeOnEscape: true,
		position: [400,100],
		overlay: 
		{
			backgroundColor: '#000',
			opacity: 0.5
		},
		buttons: 
		{
			'<?php echo ucfirst($txt->getWord('cancel')); ?>': function() 
			{ jQuery(this).dialog('close'); },
			'<?php echo ucfirst($txt->getWord('save')); ?>': function() 
			{
				jQuery.post("ajax.php?con=<?php echo $_GET['con']; ?>&editor=adbrand&adbrand="+itemId, 
				{ 
					action:'edit', 
					Title: jQuery('#editDialog #Title').val(), 
					TypeId: jQuery('#editDialog #TypeId').val(), 
					CatId: jQuery('#editDialog #CatId').val(), 
					ImageDelete: jQuery('#editDialog #ImageDelete').val(),
					Description: jQuery('#editDialog #Description').val()
				}).success(function() { loadItems(<?php echo $adc->Pager->getPageCurrent(); ?>); });
				jQuery(this).dialog('close');
			}
		}
	});
}

function deleteDialog(itemId)
{
	jQuery('#delete_dialog').dialog('open');
	jQuery('#delete_dialog').dialog({
		bgiframe: true,
		resizable: false,
		height:140,
		modal: true,
		overlay: 
		{
			backgroundColor: '#000',
			opacity: 0.5
		},
		buttons: 
		{
			'<?php echo ucfirst($txt->getWord('no')); ?>': function() 
			{
				jQuery(this).dialog('close');
			},
			'<?php echo ucfirst($txt->getWord('delete')).' '.$txt->getWord('ad_type'); ?>': function() 
			{
				jQuery.post("ajax.php?con=<?php echo $_GET['con']; ?>&list=<?php echo $_GET['list']; ?>", 
				{ 
					action:'<?php echo $commander->getPostAction(); ?>', 
					del_item_id: itemId} 
				).success(function() { loadItems(); });
				jQuery(this).dialog('close');
			}
		}
	});
}

<?php 
	$adc->loadAdBrandsCount();
	$adc->Pager->calcPagesCount();
	
	$prev = 1;
	$next = 1;
	
	if($adc->Pager->getPagesCount() > $adc->Pager->getPageCurrent())
	{ $next = ($adc->Pager->getPageCurrent() + 1); }
	else
	{ $next = ($adc->Pager->getPageCurrent()); }
	
	if(($adc->Pager->getPageCurrent() > 1) && ($adc->Pager->getPagesCount() >= $adc->Pager->getPageCurrent()))
	{ $prev = ($adc->Pager->getPageCurrent() - 1); }
?>

function loadItems(page)
{
	var typeId = <?php echo intval($_REQUEST['typeId']); ?>;
	var catId = <?php echo intval($_REQUEST['catId']); ?>;
	var getUrl = 'ajax.php<?php echo $commander->getFormAction('ajax'); ?>';
	if(!(page > 0)) { page = 1; }
	
	if($("#typeId").val() > 0)
	{ 
		typeId = $("#typeId").val();
		getUrl = getUrl + '&typeId='+typeId;
	}
	if($("#catId").val() > 0)
	{ 
		catId = $("#catId").val();
		getUrl = getUrl + '&catId='+catId;
	}
	getUrl = getUrl + '&page=' + page + '&q=<?php echo $_REQUEST['q']; ?>';

	jQuery('table.item_list tbody').empty();
	jQuery('table.item_list tbody').html('<?php echo $commander->getComponentListLoading(); ?>'); 
	jQuery.ajax(
	{
		url: getUrl,
		cache: false,
	  	success: function(html)
	  	{ 
			jQuery('table.item_list tr.loading').remove();
			jQuery("table.item_list tbody").append(html);
		}
	});
}
</script>
